package com.wind.plugin.openAction;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class CustomTextEditor implements FileEditor {

    private static final Logger LOG = Logger.getInstance(CustomTextEditor.class);
    private final Project project;
    private final VirtualFile file;
    private final JTextArea textArea;
    private final JButton saveButton;
    private final JPanel panel;

    public CustomTextEditor(Project project, VirtualFile file) {
        this.project = project;
        this.file = file;
        this.textArea = new JTextArea();
        this.saveButton = new JButton("Save");
        panel = new JBPanel(new BorderLayout());

        loadFile();
        setUpUI();
    }

    private void loadFile() {
        try {
            // 读取文件内容并显示在 textArea 中
            String content = new String(file.contentsToByteArray(), StandardCharsets.UTF_8);
            SwingUtilities.invokeLater(() -> textArea.setText(content));
        } catch (IOException e) {
            LOG.error("Failed to load file: " + file.getName(), e);
            SwingUtilities.invokeLater(() -> textArea.setText("Error loading file..."));
        }
    }

    private void setUpUI() {
        // 设置 textArea 为只读或者可编辑
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        // 使 TextArea 嵌入在滚动面板中
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane, BorderLayout.CENTER);

        // 设置保存按钮的事件
        saveButton.addActionListener(e -> saveFile());

        // 将保存按钮添加到界面下方
        panel.add(saveButton, BorderLayout.SOUTH);

    }

    private void saveFile() {
        try {
            String updatedContent = textArea.getText();
            file.setBinaryContent(updatedContent.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            LOG.error("Failed to save file: " + file.getName(), e);
            JOptionPane.showMessageDialog(null, "Error saving file", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    @Override
    public JComponent getComponent() {
        return panel;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return textArea;
    }

    @NotNull
    @Override
    public String getName() {
        return "XML图形化编辑";
    }


    @Override
    public void dispose() {
        // 清理资源
    }

    @Override
    public FileEditorLocation getCurrentLocation() {
        return null;
    }

    @Override
    public void setState(FileEditorState state) {
        // 处理编辑器状态
    }

    @Override
    public boolean isModified() {
        return false; // 你可以根据需要设置是否修改
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void addPropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {

    }

    @Override
    public void removePropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {

    }

    @Nullable
    @Override
    public <T> T getUserData(@NotNull Key<T> key) {
        return null;
    }

    @Override
    public <T> void putUserData(@NotNull Key<T> key, @Nullable T t) {

    }
}
